package com.tupu.service.impl.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tupu.common.Result;
import com.tupu.entity.admin.LogInfo;
import com.tupu.entity.permission.PermissionInfo;
import com.tupu.mapper.admin.LogMapper;
import com.tupu.mapper.permission.PermissionListMapper;
import com.tupu.service.admin.LogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, LogInfo> implements LogService {

    @Autowired(required = false)
    private LogMapper logMapper;

    @Autowired(required = false)
    private PermissionListMapper permissionMapper;

    @Override
    public void addLog(LogInfo logInfo) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        logInfo.setOperateTime(format);
        logMapper.insert(logInfo);
    }

    @Override
    public void deleteById(Integer logId) {
        logMapper.deleteById(logId);
    }

    @Override
    public Page<LogInfo> pageInfo(String name, Long page, Long size) {
        QueryWrapper<LogInfo> queryWrapper = new QueryWrapper<>();
        Page<LogInfo> pageInfo = new Page<>(page, size);
        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.like("user_name", name);
        }
        queryWrapper.orderByDesc("log_id");
        logMapper.selectPage(pageInfo, queryWrapper);
        return pageInfo;
    }

    @Override
    public Page<LogInfo> pageResult(Integer page, Integer size, String user_name) {
        QueryWrapper<LogInfo> queryWrapper = new QueryWrapper<>();
        Page<LogInfo> pageInfo = new Page<>(page, size);
        if (StringUtils.isNotEmpty(user_name)) {
            queryWrapper.like("user_name", user_name);
        }
        queryWrapper.orderByAsc("log_id");
        logMapper.selectPage(pageInfo, queryWrapper);
        List<LogInfo> records = pageInfo.getRecords();
        for (LogInfo log : records) {
            Integer permissionId = log.getPermissionId();
            PermissionInfo threePermissionInfo = permissionMapper.selectById(permissionId);
            Integer twoLevelId = threePermissionInfo.getParentId();
            PermissionInfo twoPermissionInfo = permissionMapper.selectById(twoLevelId);
            log.setThreeLevel(threePermissionInfo.getName());
            log.setTwoLevel(twoPermissionInfo.getName());
            Integer oneLevelId = twoPermissionInfo.getParentId();
            log.setOneLevel(permissionMapper.selectById(oneLevelId).getName());
        }
        return pageInfo;
    }

    @Override
    public List<LogInfo> findAll(String user_name) {

        QueryWrapper<LogInfo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(user_name)) {
            queryWrapper.like("user_name", user_name);
        }
        queryWrapper.orderByAsc("log_id");
        List<LogInfo> logInfos = logMapper.selectList(queryWrapper);
        /**
         for (LogList log : logLists) {
         Integer permissionId = log.getPermissionId();
         PermissionList threePermissionList = permissionListMapper.selectById(permissionId);
         Integer twoLevelId = threePermissionList.getParentId();
         PermissionList twoPermissionList = permissionListMapper.selectById(twoLevelId);
         log.setThreeLevel(threePermissionList.getName());
         log.setTwoLevel(twoPermissionList.getName());
         Integer oneLevelId = twoPermissionList.getParentId();
         log.setOneLevel(permissionListMapper.selectById(oneLevelId).getName());
         }
         */
        return logInfos;
    }

}
